<template>
  <div>
    <el-page-header @back="$router.back()" content="用户列表" style="margin-bottom: 20px"/>
    <el-form :model="searchFrom" ref="searchForm" inline>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="searchFrom.username" placeholder="用户名" clearable/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="searchFrom.nickname" placeholder="昵称" clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchFrom.status" clearable>
          <el-option :value="1" label="可用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button type="warning" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-bottom: 15px">
      <el-button @click="openWin()">添加</el-button>
    </div>
    <el-table :data="dataList" border>
      <el-table-column label="编号" prop="userId"/>
      <el-table-column label="用户名" prop="username"/>
      <el-table-column label="昵称" prop="nickname"/>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">可用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="openWin(scope.row.userId)">修改</el-button>
          <el-button type="warning" size="small" @click="delData(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :page-size="searchFrom.limit" :current-page="searchFrom.page" :page-sizes="[5,10,30,50,100]" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    <UserinfoUpdate ref="UserinfoUpdate" @closeWin="getDataList"/>
  </div>
</template>

<script>
import UserinfoUpdate from '@/views/userinfo/UserinfoUpdate'
export default {
  name: 'UserinfoList',
  components: { UserinfoUpdate },
  data () {
    return {
      searchFrom: {
        page: 1,
        limit: 5,
        username: '',
        nickname: '',
        status: '',
        userId: ''
      },
      dataList: [],
      total: 0
    }
  },
  methods: {
    resetForm () {
      this.$refs.searchForm.resetFields()
    },
    delData (userId) {
      this.$confirm(`确定删除编号[${userId}]的值吗?`, { type: 'info' }).then(() => {
        this.$http.delete(`user/delete/${userId}`).then(() => {
          this.$alert('删除数据成功！', { type: 'success' }).then(() => {
            this.getDataList()
          })
        })
      })
    },
    openWin (userId) {
      this.$refs.UserinfoUpdate.init(userId)
    },
    getDataList () {
      this.$http.get('user/page', this.searchFrom).then(data => {
        this.dataList = [...data.data.list]
        this.total = data.data.total
      })
    },
    handleSizeChange (val) {
      this.searchFrom.limit = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.searchFrom.page = val
      this.getDataList()
    }
  },
  created () {
    this.$nextTick(() => {
      this.$http.get('user/info').then(data => {
        this.searchFrom.userId = data.data.userId
        this.getDataList()
      })
    })
  }
}
</script>

<style scoped>

</style>
